package com.tarena.lbs.activity.rpc;

import com.tarena.lbs.activity.api.pojo.ActivityApi;
import com.tarena.lbs.activity.api.pojo.dto.ActivityDto;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@DubboService
public class ActivityApiImpl implements ActivityApi {

    @Autowired
    private ActivityService activityService;

    @Override
    public Boolean activityVisible(Integer userId, Integer activityId) {
        log.info("正在检查用户:{},是否符合活动:{},的目标人群");
        try {
            // 注意类型转换，数据库主键是Long
            return activityService.isTargetConsumer(userId, activityId);
        } catch (Exception e) {
            log.error("检查活动可见性异常", e);
            return false;
        }
    }

    @Override
    public ActivityDto getActivityById(Integer activityId) {
        try {
            ActivityVO activityVO = activityService.detail(activityId);
            if (activityVO == null) {
                return null;
            }
            ActivityDto dto = new ActivityDto();
            BeanUtils.copyProperties(activityVO, dto);
            return dto;
        } catch (BusinessException e) {
            log.error("获取活动详情异常", e);
            return null;
        }
    }
}
